<template>
  <div>
    <mt-header title="设置" fixed>
      <router-link to="/" slot="left">
        <mt-button icon="back">返回</mt-button>
      </router-link>
    </mt-header>
    <div class="form">
      <mt-field label="分销默认折扣" placeholder="请输入101~1000范围内折扣" v-model="discount"></mt-field>
      <div class="btn">
        <mt-button size="large" type="primary" @click.native="changeDiscount" style="font-size: 16px;">修改</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'setting',
  data () {
    return {
      url: {
        update: '/distributor/discount/update',
        info: '/component/mall/login/info'
      },
      discount: null,
      oldDiscount: null
    }
  },
  methods: {
    changeDiscount () {
      if (this.discount === this.oldDiscount) {
        return
      }
      this.$ajax.post(this.url.update, parseInt(this.discount)).then(res => {
        if (res.data.success) {
          this.oldDiscount = this.discount
          this.$Toast({
            message: '操作成功',
            iconClass: 'iconfont el-icon-dingdan'
          })
        } else {
          this.$Toast(res.data.errorMessage)
        }
      })
    },
    initInfo () {
      this.$ajax.post(this.url.info)
        .then(res => {
          if (res.data.success) {
            this.discount = res.data.data.distributorDiscount
            this.oldDiscount = res.data.data.distributorDiscount
          }
        })
    }
  },
  created () {
    this.initInfo()
  }
}
</script>

<style scoped>
  .form{
    margin-top: 40px;
  }
  .mint-cell-wrapper{
    border-bottom: 1px solid #dddee1;
  }
  .btn{
    padding: 8px;
  }
</style>
